/* ====================================================================
 * (#)Header: XmlUtils.java
 * (#)Revision: 
 * (#)Date Created: Mar 2, 2007 12:07:22 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.xml;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.XMLConstants;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.NamespaceContext;

import org.w3c.dom.Document;

import com.sun.org.apache.xml.internal.utils.PrefixResolver;
import com.sun.org.apache.xml.internal.utils.PrefixResolverDefault;

/**
 * <code>XmlUtils</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class XmlUtils {

	static {
		instance = new XmlUtils();
	}

	private static XmlUtils instance;

	private XmlUtils() {
	}

	public NamespaceContext createNamespaceContext(final Document document) {

		NamespaceContext context = new NamespaceContext() {

			final PrefixResolver resolver = new PrefixResolverDefault(document.getDocumentElement());
			
			public String getNamespaceURI(String prefix) {
				return resolver.getNamespaceForPrefix(prefix);
			}

			@Override
			public String getPrefix(String namespaceURI) {
				return null;
			}

			@Override
			public Iterator<String> getPrefixes(String namespaceURI) {
				return null;
			}
		};
		return context;
	}

	public NamespaceContext createNamespaceContext(Map<String, String> nameSpaces) {
		final Map<String, String> map = new HashMap<String, String>();
		if (nameSpaces != null) {
			map.putAll(nameSpaces);
		}
		NamespaceContext context = new NamespaceContext() {
			private Map<String, String> nameSpaces = map;

			public String getNamespaceURI(String prefix) {
				if (nameSpaces.containsKey(prefix)) {
					return nameSpaces.get(prefix);
				} else {
					return XMLConstants.NULL_NS_URI;
				}
			}

			@Override
			public String getPrefix(String namespaceURI) {
				return null;
			}

			@Override
			public Iterator<String> getPrefixes(String namespaceURI) {
				return null;
			}
		};

		return context;
	}

	// === utility api === //
	public XMLGregorianCalendar toXmlCalendar(String xml) {
		DatatypeFactory df = null;
		try {
			df = DatatypeFactory.newInstance();
		} catch (DatatypeConfigurationException e) {
			throw new RuntimeException(e);
		}
		XMLGregorianCalendar xmlCalendar = df.newXMLGregorianCalendar(xml);
		return xmlCalendar;
	}

	protected boolean isEmpty(String str) {
		return str == null || str.trim().length() == 0;
	}

	// === get/set === //
	public static XmlUtils getInstance() {
		return instance;
	}
}
